<template>
  <el-table :data="permisstionArr" style="width: 100%; margin-bottom: 20px" row-key="id" border>
    <el-table-column prop="name" label="名称" />
    <el-table-column prop="code" label="权限值" />
    <el-table-column prop="updateTime" label="修改时间" />
    <el-table-column label="操作">
      <template #="{ row, $index }">
        <el-button @click="addPermisstion(row)" type="primary" size="small" :disabled="row.level == 4 ? true : false">{{
          row.level == 3 ? '添加功能' :
            '添加菜单'
        }}</el-button>
        <el-button @click="updatePermisstion(row)" type="primary" :disabled="row.level == 1 ? true : false"
          size="small">编辑</el-button>
        <el-popconfirm @confirm="removeMenu(row.id)" :title="`你确定要删除${row.name}?`" width="260px">
          <template #reference>
            <el-button type="primary" :disabled="row.level == 1 ? true : false" size="small">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="dialogVisible" :title="menuData.id ? '修改菜单' : '添加菜单'">
    <el-form>
      <el-form-item label="名称">
        <el-input v-model="menuData.name" placeholder="请你输入菜单名称"></el-input>
      </el-form-item>
      <el-form-item label="权限">
        <el-input v-model="menuData.code" placeholder="请你输入权限值"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import { reqAllPermisstion, reqAddOrUpdateMenu, reqRemoveMenu } from '@/api/acl/menu';
import { PermissionResponseData, PermissionList, Permisstion, MenuParams } from '@/api/acl/menu/type';
import { ElMessage } from 'element-plus';

// 存储菜单数据
let permisstionArr = ref<PermissionList>([])
// 控制对话框显示隐藏
let dialogVisible = ref<boolean>(false)
// 携带的参数
let menuData = reactive<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0,
})

onMounted(() => {
  getHasPermisstion()
})

// 获取菜单数据
const getHasPermisstion = async (pager = 1) => {
  let result: PermissionResponseData = await reqAllPermisstion()
  if (result.code == 200) {
    permisstionArr.value = result.data
  }
}

// 添加菜单按钮
const addPermisstion = (row: Permisstion) => {
  // 清空数据
  Object.assign(menuData, {
    id: 0,
    code: '',
    level: 0,
    name: '',
    pid: 0,
  })
  dialogVisible.value = true
  // 收集新增菜单的level数据
  menuData.level = row.level + 1
  // 给谁新增子菜单
  menuData.pid = row.id
}

// 编辑菜单
const updatePermisstion = (row: Permisstion) => {
  dialogVisible.value = true
  Object.assign(menuData, row)
}

// 确定按钮
const save = async () => {
  let result = await reqAddOrUpdateMenu(menuData)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: menuData.id ? '修改成功' : '新增成功'
    })
    dialogVisible.value = false
    getHasPermisstion()
  } else {
    ElMessage({
      type: 'error',
      message: menuData.id ? '修改失败' : '新增失败'
    })
  }
}

// 删除菜单
const removeMenu = async (id: number) => {
  let result: any = await reqRemoveMenu(id)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
    getHasPermisstion()
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败'
    })
  }
}
</script>

<style lang="scss" scoped></style>